package com.cjx.springcloud.threads;


import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;

import java.util.concurrent.*;

/**
 * @author caojunxiong
 * @创建人 caojunxiong
 * @创建时间 2021/3/5
 * @描述
 */
@Slf4j
public class ThreadDemo {

    //    private static final org.slf4j.Logger log = LoggerFactory.getLogger(ThreadDemo.class);
//    private  static Logger log = LogManager.getLogger(ThreadDemo.class);

    public static void main(String[] args) throws ExecutionException, InterruptedException {

//        Spring 框架提供的 CustomizableThreadFactory。
        ThreadFactory springThreadFactory = new CustomizableThreadFactory("springThread-pool-");
        ExecutorService exec = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(10), springThreadFactory);
        exec.submit(() -> {
            log.info("--记忆中的颜色是什么颜色---");
        });


//Google guava 工具类 提供的 ThreadFactoryBuilder ,使用链式方法创建。
        ThreadFactory guavaThreadFactory = new ThreadFactoryBuilder().setNameFormat("retryClient-pool-").build();
        ExecutorService exes = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(10), guavaThreadFactory);
        exes.submit(() -> {
            log.info("--记忆中的颜色是什么颜色---");
        });
//Apache commons-lang3 提供的 BasicThreadFactory.
        ThreadFactory basicThreadFactory = new BasicThreadFactory.Builder().namingPattern("basicThreadFactory-").build();
        ExecutorService executorService = new ThreadPoolExecutor(1, 1,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(10), basicThreadFactory);
        executorService.submit(() -> {
            log.info("--记忆中的颜色是什么颜色---");
            log.debug("--记忆中的颜色是什么颜色---");
        });


    }
}

/**
 * 1、继承Thread类创建线程
 */
class MyThread extends Thread {
    @Override
    public void run() {
        System.out.println("MyThread.run()");
    }
}

/**
 *
 */
class MyThread2 implements Runnable {
    @Override
    public void run() {
        System.out.println("MyThread.run()");
    }
}

/**
 * 3、实现Callable接口通过FutureTask包装器来创建Thread线程
 */
class SomeCallable<V> implements Callable<V> {
    @Override
    public V call() throws Exception {
        return null;
    }
}
